﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ReallyReallyReallySimpleRoguelike.Entities;
using System.Drawing;
using System.Threading;
using ReallyReallyReallySimpleRoguelike.Helpers;

namespace ReallyReallyReallySimpleRoguelike.Managers
{
    class CreatureManager
    {
        private Player player;  
        private bool playerAlive;

        public IList<Monster> monsters; //Refactor 4:IList replaces List

        public CreatureManager()
        {
            player = new Player(DungeonManager.Instance.DungeonLevel.GetValidRandomPoint());
            player.CreatureDeadEvent += new Creature.CreatureDeadHandler(player_CreatureDeadEvent);
            monsters = new List<Monster>();
            playerAlive = true;
            
        }

        public void ClearMonsters()
        {
            monsters.Clear();
        }

        public void AddMonsters()
        {
            //add monsters * level
            for (int i = 0; i < Constants.NumberOfMonsters * DungeonManager.Instance.CurrentDungeonLevel; i++)
            {
                Monster monster = new Monster(DungeonManager.Instance.DungeonLevel.GetValidRandomPoint());
                monster.CreatureDeadEvent += new Creature.CreatureDeadHandler(monster_CreatureDeadEvent);
                monsters.Add(monster);
            }
        }

        public void AddMonster()
        {
            Monster monster = new Monster(DungeonManager.Instance.DungeonLevel.GetValidRandomPoint());
            monster.CreatureDeadEvent += new Creature.CreatureDeadHandler(monster_CreatureDeadEvent);
            monsters.Add(monster);
        }

        public void ProcessMonsters()
        {
            if (monsters.Count() > 0)
            {
                MoveMonsters();
                CreatureCollisions();
            }
        }

        public void CreaturesToTiles()
        {
            
            monsters.ToList().ForEach(m => DungeonManager.Instance.DungeonLevel.AddTile(m));
            DungeonManager.Instance.DungeonLevel.AddTile(player);

        }

        private void MoveMonsters()
        {
            monsters.ToList().ForEach(m =>
            {
                Point currentPlayerLocation = new Point(player.X, player.Y);
                m.MoveToPlayer(currentPlayerLocation, monsters);
            });
        }

        /// <summary>
        /// Finds if a monster an player are on the same square then calls Fight
        /// This should make it easier to test.  test 1 collision test 2 fight.
        /// </summary>
        private void CreatureCollisions()
        {
            List<Monster> monstersInBattle = monsters.Where(m => m.X == player.X && m.Y == player.Y).ToList();

            Fight(monstersInBattle);

        }

        /// <summary>
        /// Fight Monsters On PlayersTile
        /// </summary>
        /// <returns>string</returns>
        /// 
        private void Fight(List<Monster> monstersInBattle)
        {
            if (monstersInBattle.Count() > 0)
            {
                SimpleBattle.Fight(player, monstersInBattle);

                player.ImageCharacter = "*";

                DungeonManager.Instance.PlaySound(SoundList.Battle);
                DungeonManager.Instance.DungeonVisualizer.Flash(player);

                player.ImageCharacter = Constants.PlayerImage;
                player.Color = Constants.PlayerColor;

                DungeonManager.Instance.ForceRedraw();

            }
        }

        public bool PlayerAlive
        { 
            get { return playerAlive; } 
        }

        public Player Player
        {
            get { return player; }
        }

        void monster_CreatureDeadEvent(object sender)
        {
            monsters.Remove((Monster)sender);
        }

        private void player_CreatureDeadEvent(object sender)
        {
            playerAlive = false;
        }

    }
}
